home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
13990
/
13990.xpi
/
chrome
/
clickYoutubeDownloader.jar
/
content
/
clickYoutube_overlay.js
next >
Wrap
Text File
|
2010-01-14
|
9KB
|
233 lines
var clickYoutubeD = {
onLoad : function() {
clickYoutubeD.loadUserPrefs();
clickYoutubeD.checkVer();
window.document.addEventListener("DOMContentLoaded", clickYoutubeD.DOMContentLoaded, true)
},
loadUserPrefs : function() {
var Application = Components.classes["@mozilla.org/fuel/application;1"].getService(Components.interfaces.fuelIApplication);
var Extension = Application.extensions.get("YoutubeDownloader@PeterOlayev.com");
clickYoutubeD.prefs = Extension.prefs;
clickYoutubeD.userOptions = {
flv : true,
mp4 : true,
_3gp : true,
hd : true,
fullHD : true
}
},
checkVer : function() {
try {
var nsIExtensionManager = Components.classes["@mozilla.org/extensions/manager;1"].getService(Components.interfaces.nsIExtensionManager);
var addonVersion = nsIExtensionManager.getItemForID("YoutubeDownloader@PeterOlayev.com").version;
var lastCheckVersion = clickYoutubeD.prefs.getValue("addonVersion", "");
var newU = clickYoutubeD.prefs.getValue("new", true);
if (newU) {
clickYoutubeD.prefs.setValue("addonVersion", addonVersion);
clickYoutubeD.prefs.setValue("new", false);
window.setTimeout(function(){
var b = getBrowser();
b.selectedTab = b.addTab("http://www.clickyoutubedownload.com/youtube/?t=d&v=" + addonVersion);
}, 1200);
} else {
if (lastCheckVersion!=addonVersion){
clickYoutubeD.prefs.setValue("addonVersion", addonVersion);
window.setTimeout(function(){
var b = getBrowser();
b.selectedTab = b.addTab("http://www.clickyoutubedownload.com/youtube/?t=u&v=" + addonVersion);
}, 1200);
}
}
return newU;
} catch(ex) {}
},
getQueryString : function (href, ji) {
gy = href.split("&");
for (i=0;i<gy.length;i++) {
ft = gy[i].split("=");
if (ft[0] == ji) {
return ft[1];
}
}
},
getQuery : function(doc, param) {
var query = clickYoutubeD.getQueryString(doc.location.search.substring(1), param);
if ((query=="") || (query==null)) {
query = clickYoutubeD.getQueryString(doc.location.hash, param);
}
return query;
},
DOMContentLoaded : function(event) {
var doc = event.originalTarget;
var loc = doc.location;
var u = doc.location.toString();
if (loc.href.match(/http:\/\/www(|[0-9])\.(|l\.)youtube\..*\/.*/i)) {
doc.videoID = clickYoutubeD.getQuery(doc, "v");
if (doc.videoID!=null) {
doc.videoTitle = doc.title.replace(/youtube - /gi, "")
var downloadLinks = doc.createElement("div");
downloadLinks.style.cssFloat = "right"
downloadLinks.style.paddingRight="22px;"
var watchViewsDiv = doc.getElementById("watch-views-div");;
watchViewsDiv.style.width = "auto";
watchViewsDiv.parentNode.insertBefore(downloadLinks, watchViewsDiv)
if (clickYoutubeD.userOptions.flv) downloadLinks.appendChild(clickYoutubeD.setLink(doc, "FLV" ,"FLV"));
if (clickYoutubeD.userOptions.mp4) downloadLinks.appendChild(clickYoutubeD.setLink(doc, "MP4", "MP4 - 480x360"));
if (clickYoutubeD.userOptions._3gp) downloadLinks.appendChild(clickYoutubeD.setLink(doc, "3GP", "3GP - 320x240"));
if (clickYoutubeD.userOptions.hd) {
if (clickYoutubeD.isVideoSupportHD(doc)) {
downloadLinks.appendChild(clickYoutubeD.setLink(doc, "HD", "HD 720p - 1280x720"));
}
}
if (clickYoutubeD.userOptions.fullHD) {
if (clickYoutubeD.isVideoSupportFullHD(doc)) {
downloadLinks.appendChild(clickYoutubeD.setLink(doc, "/ FULL HD", "HD 1080p - 1920x1080"));
}
}
var icon = doc.createElement("div");
icon.style.backgroundImage = "url(chrome://clickYoutubeD/content/icons/icon_small.png)"
icon.style.backgroundRepeat = "no-repeat"
icon.style.width = "16px";
icon.style.height = "16px";
icon.style.cssFloat = "right"
icon.style.marginLeft = "4px"
downloadLinks.appendChild(icon);
}
}
},
isVideoSupportHD : function(doc) {
var moviePlayer = doc.getElementById('movie_player');
if (moviePlayer) {
var flashvars = moviePlayer.attributes.getNamedItem('flashvars');
if (flashvars!=null) {
if (flashvars.value.indexOf("fmt_map=22")!=-1) {
return true;
} else if (flashvars.value.indexOf("22%2F2000000%")!=-1) {
return true;
}
}
} else {
var innerHTML = doc.getElementsByTagName("html")[0].innerHTML;
if (innerHTML) {
if (innerHTML.indexOf("22%2F2000000%")!=-1) {
return true;
}
}
}
return false;
},
isVideoSupportFullHD : function(doc) {
var moviePlayer = doc.getElementById('movie_player');
if (moviePlayer) {
var flashvars = moviePlayer.attributes.getNamedItem('flashvars');
if (flashvars!=null) {
if (flashvars.value.indexOf("fmt_map=37")!=-1) {
return true;
}
}
} else {
var innerHTML = doc.getElementsByTagName("html")[0].innerHTML;
if (innerHTML) {
if (innerHTML.indexOf("37%2F4000000")!=-1) {
return true;
}
}
}
return false;
},
setLink : function(doc, type, tooltip) {
var l = doc.createElement("a");
l.style.marginLeft = "3px"
l.videoType = type;
l.videoID = doc.videoID
l.doc = doc
l.innerHTML = type
l.setAttribute("href", "#")
l.setAttribute("onclick", "return false")
if (tooltip) {
l.setAttribute("title", tooltip)
}
l.addEventListener("click",function(e) {
var target = e.currentTarget;
var url = clickYoutubeD.getYoutubeVideoUrl(target.videoID,
target.doc,
target.videoType);
if ((target.videoType.toLowerCase()=="hd") || (target.videoType.toLowerCase()=="/ full hd")) {
clickYoutubeD.downloadFile(doc.videoTitle, url, "mp4");
} else {
clickYoutubeD.downloadFile(doc.videoTitle, url, target.videoType);
}
},false);
return l;
},
getYoutubeVideoUrl : function(videoID, doc, videoType) {
var innerHTML = doc.getElementsByTagName("html")[0].innerHTML;
var reg = /\&t=[\w-]{10,}/ig;
var t = innerHTML.match(reg)
var p1 = innerHTML.indexOf("\"t\":") + 6;
var p2 = innerHTML.indexOf("\",", p1);
var t = innerHTML.substring(p1, p2 - 3);
t = "t=" + t
var moviePlayer = doc.getElementById('movie_player');
if (moviePlayer) {
var flashvars = moviePlayer.attributes.getNamedItem('flashvars');
if (flashvars!=null) {
var t = flashvars.value.match(reg)
if (t.length>0) t = t[0]
} else return false;
}
t += "=";
if (videoType.toLowerCase()=="hd") {
var downloadURL = "http://youtube.com/get_video?video_id=" + videoID + "&" + t + "&fmt=22"
} else if (videoType.toLowerCase()=="/ full hd") {
var downloadURL = "http://youtube.com/get_video?video_id=" + videoID + "&" + t + "&fmt=37"
} else if (videoType.toLowerCase()=="mp4") {
var downloadURL = "http://youtube.com/get_video?video_id=" + videoID + "&" + t + "&fmt=18"
} else if (videoType.toLowerCase()=="3gp") {
var downloadURL = "http://youtube.com/get_video?video_id=" + videoID + "&" + t + "&fmt=17"
} else {
var downloadURL = "http://youtube.com/get_video?video_id=" + videoID + "&" + t
}
return downloadURL;
},
getFileToDownload : function(fileName, fileType) {
var nsIFilePicker = Components.interfaces.nsIFilePicker;
var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
fp.init(window, "Save As", nsIFilePicker.modeSave);
fp.defaultString = fileName;
fp.appendFilter(fileType, "*." + fileType);
var rv = fp.show();
if (rv == nsIFilePicker.returnOK || rv == nsIFilePicker.returnReplace) {
var file = fp.file;
var path = fp.file.path;
return file;
}
return null;
},
downloadFile : function(title, url, fileType) {
if (fileType.toLowerCase()=="hd") title += "_hd";
var fileName = title +"."+fileType
var file = clickYoutubeD.getFileToDownload(fileName, fileType);
var persist = Components.classes['@mozilla.org/embedding/browser/nsWebBrowserPersist;1'].createInstance(Components.interfaces.nsIWebBrowserPersist);
var ios = Components.classes['@mozilla.org/network/io-service;1'].getService(Components.interfaces.nsIIOService);
var xfer = Components.classes["@mozilla.org/transfer;1"].createInstance(Components.interfaces.nsITransfer);
var uri = ios.newURI(url, null, null);
var target = ios.newFileURI(file);
xfer.init(uri, target, "", null, null, null, persist);
persist.progressListener = xfer;
persist.saveURI(uri, null, null, null, null, file);
}
}
window.addEventListener("load", clickYoutubeD.onLoad, false);